package chapter14;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/22 17:28
 */
public class BasicQueueTest {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newSingleThreadExecutor();
        exec.submit(() -> {
            int i = 0;
            Random random = new Random(47);
            try {
                IBasicQueue queue = new BasicQueue("./", "task");
                while (!Thread.interrupted()) {
                    String msg = "task " + (i++);
                    queue.enqueue(msg.getBytes("UTF-8"));
                    System.out.println("produce: " + msg);
                    TimeUnit.MILLISECONDS.sleep(random.nextInt(1000));
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                System.out.println("exit via interrupt");
            }
        });

        TimeUnit.SECONDS.sleep(10);
        exec.shutdownNow();

    }

}
